package com.shu.logsearch.common.web;

import cn.hutool.http.HttpStatus;
import com.github.pagehelper.Page;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Tree Cai
 * @date 2019/7/11
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
    /**
     * 是否请求成功
     */
    private boolean success;
    /**
     * 返回的消息
     */
    private String msg;
    /**
     * 返回的状态码
     */
    private Integer code;
    /**
     * 请求返回的数据
     */
    private Map<String,Object> data = new HashMap<>();


    public Result(String msg, Boolean success) {
        setMsg(msg);
        setSuccess(success);
    }

    public Result(Integer code, String msg, Boolean success) {
        setCode(code);
        setMsg(msg);
        setSuccess(success);
    }

    public Boolean success() {
        return success;
    }

    /**
     * 默认返回结果
     *
     * @param success
     * @return
     */
    public static Result DEFAULT_RESULT(Boolean success) {
        return success ? SUCCESS() : ERROR();
    }

    /**
     * 默认的成功返回值
     *
     * @return
     */
    public static Result SUCCESS() {
        return SUCCESS("操作成功");
    }

    /**
     * 默认的错误返回值
     *
     * @return
     */
    public static Result ERROR() {
        return ERROR("操作失败");
    }

    /**
     * 错误返回值
     *
     * @return
     */
    public static Result ERROR(String msg) {
        return new Result(HttpStatus.HTTP_INTERNAL_ERROR, msg, false);
    }

    /**
     * 成功返回值
     *
     * @return
     */
    public static Result SUCCESS(String msg) {
        return new Result(HttpStatus.HTTP_OK, msg, true);
    }

    /**
     * 分页结果
     * @param Page
     * @return
     */
    public static Result PAGE(Page page) {
        Result result = SUCCESS();
        Map<String,Object> data = new HashMap<>();
        data.put("pageNum",page.getPageNum());
        data.put("pageSize",page.getPageSize());
        data.put("totalPage",page.getTotal());
        data.put("queryData",page);
        result.setData(data);
        return result;
    }

    /**
     * 空页
     * @return
     */
    public static Result EMPTY_PAGE() {
        Result result = SUCCESS();
        Map<String,Object> data = new HashMap<>();
        data.put("pageNum",0);
        data.put("pageSize",0);
        data.put("totalPage",0);
        data.put("queryData",null);
        result.setData(data);
        return result;
    }
}
